//
//  SettingVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/9/17.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "SettingVC.h"
#import "YBContentModel.h"
#import "EVCPopInputView.h"
#import "ZXConfirmButton.h"
#import "YBApiManager+Mine.h"
#import <LKDBHelper/LKDBHelper.h>
#import "CacheHelper.h"
#import "SessionItem.h"

@interface SettingVC ()
@property (nonatomic, strong) ZXConfirmButton *quitLoginBtn;

@property (nonatomic, copy) NSString *originType;

@end

@implementation SettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self.tableView customRegisterClassWithName:@"MineCell"];
    self.quitLoginBtn = [ZXConfirmButton confirmButton];
    self.quitLoginBtn.k_title(@"退出登录");
    self.quitLoginBtn.frame = CGRectMake(50, 30, ScreenWidth-100, 40);
    [self.quitLoginBtn addTarget:self action:@selector(didClickBottomBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.quitLoginBtn];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
        make.bottom.equalTo(self.quitLoginBtn.mas_top);
    }];
    [self.quitLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(50);
        make.right.bottom.mas_offset(-50);
        make.height.mas_equalTo(40);
    }];
    [self setupDatasource];
}

- (void)setupDatasource {
    NSArray *titles = @[@"允许所有人", @"需要验证", @"不允许"];
//    NSString *buildVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
    NSString *shortVerstion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSString *versionStr = [NSString stringWithFormat:@"V%@", shortVerstion];
    NSArray *dataArr = @[
                         @{@"title": @"添加好友验证",
                           @"rightText": titles[kUsermanager.me.add_type - 1],
                           @"icon": @"arrow_right",
                           @"vc": @""
                           },
                         @{@"title": @"消息字体大小",
                           @"rightText": kUsermanager.msgFontSize?:@"中",
                           @"icon": @"arrow_right",
                           @"vc": @""
                           },
                         @{@"title": @"当前版本",
                           @"rightText": versionStr,
                           @"icon": @"",
                           @"vc": @""
                           },
                         @{@"title": @"更换手机号",
                           @"rightText": @"",
                           @"icon": @"arrow_right",
                           @"vc": @"ChangeMobileVC"
                           },
                         @{@"title": @"一键清理",
                           @"rightText": @"",
                           @"icon": @"arrow_right",
                           @"vc": @""
                           }
                         ];
    
    self.dataSource = [YBContentModel mj_objectArrayWithKeyValuesArray:dataArr];
    for (YBContentModel *obj in self.dataSource) {
        obj.cellId = @"MineCell";
    }
    [self.tableView reloadData];
}

- (void)didClickBottomBtnAction:(UIButton *)sender {
    [LBXAlertAction showAlertWithTitle:@"提示" msg:@"确认退出登录？" buttonsStatement:@[@"取消", @"确认"] chooseBlock:^(NSInteger buttonIdx) {
        if (buttonIdx == 0) {
            return ;
        }
        [MBProgressHUD showActivityMessageInWindow:@""];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            //            [kAppDelegate enterLoginVC];
            [[NSNotificationCenter defaultCenter] postNotificationName:notification_TokenFailed object:nil];
        });
    }];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YBContentModel *model = [self.dataSource objectAtIndex:indexPath.row];
    
   if ([model.title isEqualToString:@"消息字体大小"] == YES) {
        [LBXAlertAction showActionSheetWithTitle:@"选择消息字体" message:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitle:@[@"大", @"中", @"小"] chooseBlock:^(NSInteger buttonIdx) {
            if (buttonIdx == 1) {//大
                kUsermanager.msgFontSize = @"大";
            } else if (buttonIdx == 2) {// 女
                kUsermanager.msgFontSize = @"中";
            } else if (buttonIdx == 3) {
                kUsermanager.msgFontSize = @"小";
            }
            model.rightText = kUsermanager.msgFontSize;
            [self.tableView reloadData];
        }];
   }
   else if ([model.title isEqualToString:@"添加好友验证"] == YES) {
       NSArray *titles = @[@"允许所有人", @"需要验证", @"不允许"];
       [LBXAlertAction showActionSheetWithTitle:@"选择验证方式" message:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitle:titles chooseBlock:^(NSInteger buttonIdx) {
           if (buttonIdx == 0) {
               return ;
           }
           if (buttonIdx == 1) {//
               
           } else if (buttonIdx == 2) {// 女
               
           } else if (buttonIdx == 3) {
               
           }
           [self changeAddFriendType:buttonIdx];
           
           model.rightText = titles[buttonIdx - 1];
           [self.tableView reloadData];
       }];
   }
   else if ([model.title isEqualToString:@"一键清理"] == YES) {
       [LBXAlertAction showAlertWithTitle:@"提示" msg:@"确认清理全部聊天记录和缓存？" buttonsStatement:@[@"取消", @"确认"] chooseBlock:^(NSInteger buttonIdx) {
           if (buttonIdx == 1) {
               [self cleanAllMsgAndFile];
           }
       }];
   }
   else if (model.vc.isHaveStr == YES) {
       [self.navigationController pushViewController:[NSClassFromString(model.vc) new] animated:YES];
   }
}
- (void)changeAddFriendType:(NSInteger)type {
    
    [MBProgressHUD showActivityMessageInView:@""];
    
    [[YBApiManager.shareManager changeAddFriendType:type] subscribeNext:^(id  _Nullable x) {
        [MBProgressHUD hideHUD];
        
        [kUsermanager updateUserInfo];
        
    } error:^(NSError * _Nullable error) {
        [MBProgressHUD hideHUD];
        if ([error isServerError]) {
            [MBProgressHUD showTipMessageInView:error.localizedDescription timer:1.0f];
        } else {
            [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
        }
        [self setupDatasource];
    }];
}

- (void)cleanAllMsgAndFile {
    [MBProgressHUD showActivityMessageInWindow:@"一键清理..."];
    [MsgItem deleteWithWhere:nil];
    [SessionItem deleteWithWhere:nil];
    [Cache_Help cleanAllFiles];
    [[NSNotificationCenter defaultCenter] postNotificationName:notification_CleanAll object:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUD];
    });
}

@end
